@echo off
chcp 65001 >nul
setlocal enabledelayedexpansion

echo ==========================================
echo OpenVPN 监控系统 Docker 构建脚本 (Windows)
echo ==========================================

:: 检查参数
if "%1"=="" set "ACTION=build"
if "%1"=="build" set "ACTION=build"
if "%1"=="start" set "ACTION=start"
if "%1"=="stop" set "ACTION=stop"
if "%1"=="restart" set "ACTION=restart"
if "%1"=="logs" set "ACTION=logs"
if "%1"=="clean" set "ACTION=clean"
if "%1"=="backup" set "ACTION=backup"

:: 检查Docker是否安装
echo 检查Docker环境...
docker --version >nul 2>&1
if errorlevel 1 (
    echo 错误: Docker未安装
    exit /b 1
)

docker-compose --version >nul 2>&1
if errorlevel 1 (
    echo 错误: Docker Compose未安装
    exit /b 1
)

echo ✓ Docker环境检查通过

:: 根据动作执行相应操作
if "%ACTION%"=="build" goto :build
if "%ACTION%"=="start" goto :start
if "%ACTION%"=="stop" goto :stop
if "%ACTION%"=="restart" goto :restart
if "%ACTION%"=="logs" goto :logs
if "%ACTION%"=="clean" goto :clean
if "%ACTION%"=="backup" goto :backup
goto :usage

:build
echo 创建必要目录...
if not exist "data" mkdir data
if not exist "config" mkdir config
if not exist "backup" mkdir backup
echo ✓ 目录创建完成

echo 停止现有容器...
docker-compose down >nul 2>&1
echo ✓ 容器已停止

echo 清理旧镜像...
docker image prune -f >nul 2>&1
echo ✓ 镜像清理完成

echo 构建Docker镜像...
docker-compose build --no-cache
if errorlevel 1 (
    echo ✗ 镜像构建失败
    exit /b 1
)
echo ✓ 镜像构建完成

echo 启动服务...
docker-compose up -d
if errorlevel 1 (
    echo ✗ 服务启动失败
    exit /b 1
)
echo ✓ 服务启动完成

echo 等待服务就绪...
set /a attempts=0
:wait_loop
set /a attempts+=1
if %attempts% gtr 30 (
    echo ✗ 服务启动超时
    exit /b 1
)
curl -f http://localhost:5000/api/online-users >nul 2>&1
if errorlevel 1 (
    echo -n .
    timeout /t 2 /nobreak >nul
    goto :wait_loop
)
echo ✓ 服务已就绪
goto :show_status

:start
echo 启动服务...
docker-compose up -d
if errorlevel 1 (
    echo ✗ 服务启动失败
    exit /b 1
)
echo ✓ 服务启动完成
goto :show_status

:stop
echo 停止服务...
docker-compose down
echo ✓ 服务已停止
exit /b 0

:restart
echo 重启服务...
docker-compose down >nul 2>&1
docker-compose up -d
if errorlevel 1 (
    echo ✗ 服务重启失败
    exit /b 1
)
echo ✓ 服务重启完成
goto :show_status

:logs
echo 查看日志...
docker-compose logs -f openvpn-monitor
exit /b 0

:clean
echo 清理所有容器和镜像...
docker-compose down -v >nul 2>&1
docker image prune -f >nul 2>&1
docker system prune -f >nul 2>&1
echo ✓ 清理完成
exit /b 0

:backup
echo 备份数据库...
if exist "data\openvpn_monitor.db" (
    set "timestamp=%date:~0,4%%date:~5,2%%date:~8,2%_%time:~0,2%%time:~3,2%%time:~6,2%"
    set "timestamp=!timestamp: =0!"
    copy "data\openvpn_monitor.db" "backup\openvpn_monitor_!timestamp!.db" >nul
    echo ✓ 备份完成
) else (
    echo 数据库文件不存在，跳过备份
)
exit /b 0

:show_status
echo.
echo 服务状态:
docker-compose ps
echo.
echo 访问地址:
echo http://localhost:5000
echo.
echo 查看日志:
echo docker-compose logs -f openvpn-monitor
echo.
echo 停止服务:
echo docker-compose down
exit /b 0

:usage
echo 用法: %0 {build^|start^|stop^|restart^|logs^|clean^|backup}
echo.
echo 命令说明:
echo   build   - 构建镜像并启动服务（默认）
echo   start   - 启动服务
echo   stop    - 停止服务
echo   restart - 重启服务
echo   logs    - 查看日志
echo   clean   - 清理所有容器和镜像
echo   backup  - 备份数据库
exit /b 1 